using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using LuaInterface;

namespace LuaFramework {
    public class PanelManager : Manager {
        private Transform parent;

        Transform Parent {
            get {
                if (parent == null) {
                    GameObject go = GameObject.FindWithTag("GuiCamera");
                    if (go != null) parent = go.transform;
                }
                return parent;
            }
        }

        /// <summary>
        /// 创建面板，请求资源管理器
        /// </summary>
        /// <param name="type"></param>
        public void CreatePanel(string name, LuaFunction func = null) {
            int dwIndex = name.IndexOf('.');
            string abName = name.Substring(0, dwIndex) + AppConst.ExtName;
            string assetName = name.Substring(dwIndex + 1);
            if (Parent.Find(name) != null) return;

#if ASYNC_MODE
            ResManager.LoadPrefab(abName, assetName, delegate(UnityEngine.Object[] objs) {
                if (objs.Length == 0) return;
                GameObject prefab = objs[0] as GameObject;
                if (prefab == null) return;

                GameObject go = Instantiate(prefab) as GameObject;
                go.name = name;
                //go.layer = LayerMask.NameToLayer("UI");
                go.transform.SetParent(Parent);
                go.transform.localPosition = prefab.transform.localPosition;
                RectTransform rectgo = go.GetComponent<RectTransform>();
                if (rectgo != null)
                {
                    rectgo.offsetMin = prefab.GetComponent<RectTransform>().offsetMin;
                    rectgo.offsetMax = prefab.GetComponent<RectTransform>().offsetMax;
                }
                go.AddComponent<LuaBehaviour>();
                if (func != null) func.Call(go);
                //Debug.LogWarning("CreatePanel::>> " + name + " " + prefab);
            });
#else
            GameObject prefab = ResManager.LoadAsset<GameObject>(name, assetName);
            if (prefab == null) return;

            GameObject go = Instantiate(prefab) as GameObject;
            go.name = name;
            go.layer = LayerMask.NameToLayer("UI");
            go.transform.SetParent(Parent);
            go.transform.localPosition = prefab.transform.localPosition;
            RectTransform rectgo = go.GetComponent<RectTransform>();
            if (rectgo != null)
            {
                rectgo.offsetMin = prefab.GetComponent<RectTransform>().offsetMin;
                rectgo.offsetMax = prefab.GetComponent<RectTransform>().offsetMax;
            }
            go.AddComponent<LuaBehaviour>();
            if (func != null) func.Call(go);
            Debug.LogWarning("CreatePanel::>> " + name + " " + prefab);
#endif
        }

        /// <summary>
        /// 关闭面板
        /// </summary>
        /// <param name="name"></param>
        public void ClosePanel(string name) {
            var panelObj = Parent.Find(name);
            if (panelObj == null) return;
            Destroy(panelObj.gameObject);
        }
    }
}